package com.njust.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.njust.domain.dto.TestJoinDTO;
import com.njust.domain.dto.TestPageQueryDTO;
import com.njust.domain.dto.TestUpdateDTO;
import com.njust.domain.dto.TestUploadDTO;
import com.njust.domain.po.ComparisonTest;
import com.njust.domain.po.TestPerson;
import com.njust.domain.vo.TestPageQueryPersonVO;
import com.njust.domain.vo.TestPageQueryVO;
import com.njust.exception.IsTakingThisTestException;
import com.njust.mapper.ComparisonTestMapper;
import com.njust.mapper.TestPersonMapper;
import com.njust.service.TestService;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.sql.Wrapper;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Service
public class TestServiceImpl implements TestService {
    @Autowired
    private ComparisonTestMapper comparisonTestMapper;
    @Autowired
    private TestPersonMapper testPersonMapper;

    public void upload(TestUploadDTO testUploadDTO) {
        int projectId = testUploadDTO.getProject_id();
        String testFileLink = testUploadDTO.getTest_file_link();
        ComparisonTest comparisonTest = ComparisonTest.builder().test_file_link(testFileLink).project_id(projectId).build();
        // 判断是否已经存在方案 如果存在则更新 不存在则保存
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("project_id", projectId);
        if (comparisonTestMapper.exists(wrapper)) {
            comparisonTestMapper.update(comparisonTest, wrapper);
        } else {
            comparisonTestMapper.insert(comparisonTest);
        }
    }

    public void joinInTest(TestJoinDTO testJoinDTO) {
        int uId = testJoinDTO.getUId();
        int projectId = testJoinDTO.getProject_id();
        //检测该人员是否正在参与该测试
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("uId", uId);
        queryWrapper.eq("project_id", projectId);
        queryWrapper.eq("test_status", 1);
        if (testPersonMapper.exists(queryWrapper)) {
            throw new IsTakingThisTestException("正在参与该比对测试，不能再次申请");
        }
        // 获取日期
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        Date date = new Date();
        String dateString = format.format(date);

        TestPerson testPerson = TestPerson.builder().test_time(dateString).uId(uId).project_id(projectId).test_status(1).build();
        testPersonMapper.insert(testPerson);
    }
    @Override
    public TestPageQueryVO pageQuery(TestPageQueryDTO testPageQueryDTO) {
        PageHelper.startPage(testPageQueryDTO.getPage(), testPageQueryDTO.getPageSize());

        Page<TestPageQueryPersonVO> page = testPersonMapper.pageQuery(testPageQueryDTO);
        long total = page.getTotal();
        List<TestPageQueryPersonVO> records = page.getResult();

        return TestPageQueryVO.builder().total(total).records(records).build();
    }
    @Override
    public void update(int status, TestUpdateDTO testUpdateDTO) {
        QueryWrapper wrapper = new QueryWrapper();
        int test_person_id = testUpdateDTO.getTest_person_id();
        wrapper.eq("test_person_id", test_person_id);
        TestPerson testPerson = testPersonMapper.selectOne(wrapper);
        if (status == 2) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
            Date date = new Date();
            String completion_date = format.format(date);
            testPerson.setCompletion_date(completion_date);
        }
        testPerson.setTest_status(status);
        testPersonMapper.update(testPerson, wrapper);


    }


}
